package com.dusk.util
{
	import flash.profiler.showRedrawRegions;
	import flash.filesystem.File;
	import flash.geom.Point;
	import flash.system.ApplicationDomain;
	import flash.system.Capabilities;
	import flash.system.IME;
	import flash.system.IMEConversionMode;
	import flash.system.LoaderContext;
	import flash.system.System;
	
	public final class SystemUtil extends UtilBase
	{
		public function SystemUtil()
		{
		}
		
		public static const IME_CHINESE:String = IMEConversionMode.CHINESE;
		
		public static const IME_HALF:String = IMEConversionMode.ALPHANUMERIC_HALF;
		
		public static const IME_FULL:String = IMEConversionMode.ALPHANUMERIC_FULL;
		
		public static const DESKTOP:String = "Desktop";
		
		public static const ACTIVE_X:String = "ActiveX";
		
		public static const PLUGIN:String = "PlugIn";
		
		public static const STAND_ALONE:String = "StandAlone";
		
		public static const EXTERNAL:String = "External";
		
		public static function isDebugger():Boolean
		{
			return Capabilities.isDebugger;
		}
		
		public static function hasStreamingAudio():Boolean
		{
			return Capabilities.hasStreamingAudio;
		}
		
		public static function getOSVersion():String
		{
			return Capabilities.os;
		}
		
		public static function setIMEMode(mode:String):Boolean
		{
			if (!IMEAvailable) return false;
			try
			{
				IME.conversionMode = mode;
			}
			catch (e:Error)
			{
				trace(e.message);
				return false;
			}
			return true;
		}
		
		public static function setIMEEnabled(isEnable:Boolean):Boolean
		{
			if (!IMEAvailable) return false;
			try
			{
				IME.enabled = isEnable;
			}
			catch (e:Error)
			{
				trace(e.message);
				return false;
			}
			return true;
		}
		
		public static function IMEAvailable():Boolean
		{
			return Capabilities.hasIME && IME.isSupported;
		}
		
		public static function getPlayerVersion():String
		{
			return Capabilities.version;
		}
		
		public static function getPlayerType():String
		{
			return Capabilities.playerType;
		}
		
		public static function isApplication():Boolean
		{
			return getPlayerType() == DESKTOP;
		}
		
		public static function isPlayer():Boolean
		{
			var str1:String = getPlayerType();
			return str1 == ACTIVE_X || str1 == PLUGIN || str1 == STAND_ALONE || str1 == EXTERNAL;
		}
		
		public static function getScreenResolution():Point
		{
			return new Point(Capabilities.screenResolutionX,Capabilities.screenResolutionY);
		}
		
		public static function getAppMemory():Number
		{
			return MathUtil.decimal(System.privateMemory / (1024 * 1024));
		}
		
		public static function getAVM2Memory():Number
		{
			return MathUtil.decimal(System.totalMemoryNumber / (1024 * 1024));
		}
		
		public static function getMemoryPercent():Number
		{
			return MathUtil.decimal(System.totalMemoryNumber / System.privateMemory);
		}
		
		public static function showRenderArea():void
		{
			showRedrawRegions(true);
		}
		
		public static function isRunInTempDirectory():Boolean
		{
			return File.applicationDirectory.nativePath.indexOf("AppData\\Local\\Temp") +
					File.applicationDirectory.nativePath.indexOf("AppData/Local/Temp") != -2;
		}
		
		public static function getLoaderContext(rootDomain:ApplicationDomain = null):LoaderContext
		{
			var lc:LoaderContext = new LoaderContext(false, rootDomain);
			if(lc.hasOwnProperty("allowCodeImport"))
			{
				lc.allowCodeImport = true;
			}
			if(lc.hasOwnProperty("allowLoadBytesCodeExecution"))
			{
				lc.allowLoadBytesCodeExecution = true;
			}
			return lc;
		}
	}
}
